const http = require('../../utils/http.js'); 
const app = getApp()
const TITLE_HEIGHT = 30
const ANCHOR_HEIGHT = 18
Page({
  data: {
    imgPath: 'https://mocar.img.xzozo.com/img/icon/home/',
    carTypeDataIndex: 0,
    toView:'',
    logs: [],
    scrollTop:0,
    HOT_SINGER_LEN:10,
    listHeight:[],
    currentIndex:0,
    fixedTitle:'',
    fixedTop:0,
    showModalStatus: false,
    lockerNmae: false,
    lockerNmae1: false,
    seriesData: '',
    lockerTitle: '',
    carTypeData: ''
  },
  onLoad: function () {
    var that = this,
        list = that.data.list;
    wx.hideLoading()
  },
  onShow(){
    http.postRequest('/mini/v1/car_owner/brands',{},res=> {
      let list = [];
      for(let i in res.data) {
        for(let z in res.data[i]) {
          list.push({'index': res.data[i][z].letter, "name": res.data[i][z].brandName, "img": res.data[i][z].brandLogo, brandId: res.data[i][z].brandId})
        }
      }
      this.setData({
        logs: this._normalizeSinger(list)
      })
      this._calculateHeight()
    })
  },
  // 选择品牌
  Carseries(e) {
    let brand_id = e.currentTarget.dataset.brandid;
    http.postRequest('/mini/v1/car_owner/series',{brand_id: brand_id},res=> {
      if(res.data.length > 0) {
        this.setData({
          seriesData: res.data,
          lockerNmae: true,
          lockerTitle: e.currentTarget.dataset.name
        })
      } else {
        const pages = getCurrentPages()
        const prevPage = pages[pages.length-2] // 上一页
        // 调用上一个页面的setData 方法，将数据存储
        prevPage.setData({
          testdata: {
            id: brand_id,
            type: 'pinpai'
          }
        })
        wx.navigateBack({delta: 1});
      }
    })
  },
  // 退出右弹窗
  hidell() {
    this.setData({
      lockerNmae: false
    })
  },
  // 退出左弹窗
  hidell1() {
    this.setData({
      lockerNmae1: false
    })
  },
  // 选择车系
  carType(e) {
    let series_id = e.currentTarget.dataset.seriesid;
    http.postRequest('/mini/v1/car_owner/car_type',{ series_id: series_id },res=> {
      if(res.data.length) {
        this.setData({
          carTypeData: res.data,
          lockerNmae1:true
        })
      } else {
        const pages = getCurrentPages()
        const prevPage = pages[pages.length-2] // 上一页
        // 调用上一个页面的setData 方法，将数据存储
        prevPage.setData({
          testdata: {
            id: series_id,
            type: 'series'
          }
        })
        wx.navigateBack({delta: 1});
      }
    })
  },
  // 选择车型
  car(e) {
    let carTypeid = e.currentTarget.dataset.cartypeid;
    const pages = getCurrentPages()
    const prevPage = pages[pages.length-2] // 上一页
    // 调用上一个页面的setData 方法，将数据存储
    prevPage.setData({
      testdata: {
        id: carTypeid,
        type: 'type'
      }
    })
    wx.navigateBack({delta: 1});
  },
  _normalizeSinger(list) {
    let map = {
      hot: {
        title: '',
        items: []
      }
    }
    list.forEach((item, index) => {
      const key = item.index
      if (!map[key]) {
        map[key] = {
          title: key,
          items: []
        }
      }
      map[key].items.push({
        name: item.name,
        avatar: item.img,
        brandId: item.brandId
      })
    })
    let ret = []
    let hot = []
    for (let key in map) {
      let val = map[key]
      if (val.title.match(/[a-zA-Z]/)) {
        ret.push(val)
      }
    }
    ret.sort((a, b) => {
      return a.title.charCodeAt(0) - b.title.charCodeAt(0)
    })
    return hot.concat(ret)
  },
  scroll :function(e) {
    var newY = e.detail.scrollTop;
    this.scrollY(newY);
  },
  scrollY(newY) {
    const listHeight = this.data.listHeight
    if (newY == 0 || newY < 0) {
      this.setData({
        currentIndex:0,
        fixedTitle:''
      })
      return
    }
    for (let i = 0; i < listHeight.length - 1; i++) {
      let height1 = listHeight[i]
      let height2 = listHeight[i + 1]
      if (newY >= height1 && newY < height2) {
        this.setData({
          currentIndex:i,
          fixedTitle:this.data.logs[i].title
        })
        this.fixedTt(height2 - newY);
        return
      }
    }
    this.setData({
      currentIndex: listHeight.length - 2,
      fixedTitle: this.data.logs[listHeight.length - 2].title
    })
  },
  fixedTt(newVal) {
    let fixedTop = (newVal > 0 && newVal < TITLE_HEIGHT) ? newVal - TITLE_HEIGHT : 0
    if (this.data.fixedTop === fixedTop) {
      return
    }
    this.setData({
      fixedTop:fixedTop
    })
  },
  _calculateHeight() {
    var lHeight = [],
        that = this;
    let height = 0;
    lHeight.push(height);
      var query = wx.createSelectorQuery();
      query.selectAll('.list-group').boundingClientRect(function(rects){
        var rect = rects,
            len = rect.length;
        for (let i = 0; i < len; i++) {
          height += rect[i].height;
          lHeight.push(height)
        }
        
      }).exec();
    var calHeight = setInterval(function(){
      if (lHeight != [0]) {
        that.setData({
          listHeight: lHeight
        });
        clearInterval(calHeight);
      } 
    },1000)
  },
  scrollToview(e){
    var id = e.target.dataset.id
    this.setData({
      toView: id
    })
  },
})
